package com.leyou.item.web;

import com.leyou.item.dto.CategoryDTO;
import com.leyou.item.entity.Category;
import com.leyou.item.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("/category")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    /**
     * 根据分类的父id查询对应的子的集合
     * @param pid
     * @return
     */
    @GetMapping("/of/parent")
    public ResponseEntity<List<CategoryDTO>> listCategoryByPid(@RequestParam("pid") Long pid) {
            return ResponseEntity.ok(this.categoryService.listCategoryByPid(pid));
    }
    /**
     * 根据父类目id查询子类目的集合
     * @param pid 父类目的id
     * @return 子类目的集合
     */
//    @GetMapping("/of/parent")
//    public ResponseEntity<List<CategoryDTO>> queryCategoryByParentId(@RequestParam("pid") Long pid){
//        return ResponseEntity.ok(// 封装并返回
//                CategoryDTO.convertEntityList(// 把PO集合转为DTO集合
//                        categoryService.query().eq("parent_id", pid).list()// 根据父类目id查询集合
//                )
//        );
//    }

    /**
     * 根据分类的id查询商品的分类
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public ResponseEntity<CategoryDTO> queryCategoryById(@PathVariable("id") Long id) {
        return ResponseEntity.ok(this.categoryService.queryCategoryById(id));
    }

    /**
     * 根据id的集合查询对应的分类对象集合
     * @param ids
     * @return
     */
    @GetMapping("/list")
    public ResponseEntity<List<CategoryDTO>> listCategoryByIds(@RequestParam("ids")List<Long> ids) {
        return ResponseEntity.ok(this.categoryService.listCategoryByIds(ids));
    }

    /**
     * 根据品牌id，查询对应的分类集合
     * @param id
     * @return
     */
    @GetMapping("/of/brand")
    public ResponseEntity<List<CategoryDTO>> listCategoryByBrand(@RequestParam("id")Long id) {
        return ResponseEntity.ok(this.categoryService.listCategoryByBrand(id));
    }
}
